const ybuser = {
	baseUrl: 'api.sky-yiho.com/',
	userid: 0,
	token: '',
	tokeninfo: {},
	userid: 0,
	
	setToken:function(data){
		console.log("设置token");
		console.log(data);
		this.tokeninfo = data;
		this.token = data.token;
		this.userid = data.userid;
		uni.setStorageSync('ttyhtoken',JSON.stringify(data));
	},
	
	setUrl:function(url){
		if(url.startsWith("?s=")){
			return "https://" + this.baseUrl + url;
		}else{
			return "https://" + this.baseUrl + "?s=" + url;
		}
	},
	
	sign:function(url,data){
		if(!data){
			data = {};
		}
		if(this.token){
			
		}else{
			let tokendata = uni.getStorageSync('ttyhtoken');
			if(tokendata){
				this.tokeninfo = JSON.parse(tokendata);
				this.token = this.tokeninfo.token;
				this.userid = this.tokeninfo.userid;
			}
		}
		data.token = this.token;
		return data;
	},
	
	request:function(url,data,method,success,error){
		const that = this;
		uni.request({
			url: this.setUrl(url),
			data: this.sign(url,data),
			method: method,
			dataType: "json",
			success(res){
				if(res.statusCode == 200){
					if(res.data.ret == 200){
						success(res.data.data)
					}else{
						error(res.data.msg)
					}
				}else{
					error(res)
				}
			},
			error(e){
				error(e)
			}
		})
	},
	httpget:function(url,data,success,error){
		this.request(url,data,'GET',success,error)
	},
	httppost:function(url,data,success,error){
		this.request(url,data,"POST",success,error)
	}
}

export {ybuser}